#!/bin/sh /etc/rc.common

START=90
STOP=10

enabled="$(uci -q get baidupcs-web.config.enabled)"
port="$(uci -q get baidupcs-web.config.port)"
download_dir="$(uci -q get baidupcs-web.config.download_dir)"
max_download_rate="$(uci -q get baidupcs-web.config.max_download_rate || echo '0')"
max_upload_rate="$(uci -q get baidupcs-web.config.max_upload_rate || echo '0')"
max_download_load="$(uci -q get baidupcs-web.config.max_download_load || echo '1')"
max_parallel="$(uci -q get baidupcs-web.config.max_parallel || echo '8')"

start() {
	stop
	[ "$enabled" == "1" ] || exit 0
	mkdir -p "${download_dir}"
	baidupcs-web config set                         \
	--savedir               "${download_dir}"       \
	--max_download_rate     "${max_download_rate}"  \
	--max_upload_rate       "${max_upload_rate}"    \
	--max_download_load     "${max_download_load}"  \
	--max_parallel          "${max_parallel}"       \
	--max_upload_parallel   "${max_parallel}"       \
	>/dev/null 2>&1

	baidupcs-web web --port "${port}" --access >/dev/null 2>&1 &
}

stop() {
	killall baidupcs-web >/dev/null 2>&1
}
